package com.zh.baselibrary.model;

import android.app.AlarmManager;
import android.content.Context;
import android.os.SystemClock;

import com.zh.baselibrary.api.ITaskBusiness;
import com.zh.baselibrary.api.ITaskLoginService;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.netutils.BaseResp;
import com.zh.baselibrary.netutils.HRetrofitNetHelper;
import com.zh.baselibrary.utils.JsonUtils;
import com.zh.baselibrary.utils.LogUtils;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.EquipmentLogin;
import com.zh.model.Errata;
import com.zh.model.home.TokenBean;
import com.zh.model.request.LoginInfo;
import com.zh.model.task.SubjectBean;
import com.zh.model.task.error.ErrataSwipeResBean;
import com.zh.model.task.error.ErrorDetailBean;
import com.zh.model.task.error.ErrorDetailVOBean;
import com.zh.model.task.home.CabinetBean;
import com.zh.model.task.home.ClassCabinetBean;
import com.zh.model.task.home.CommitBean;
import com.zh.model.task.home.CommitResBean;
import com.zh.model.task.home.ErrataSwipeBean;
import com.zh.model.task.home.RemoteOpenResVO;
import com.zh.model.task.home.ReportVersionBean;
import com.zh.model.task.home.SwipingCardBean;
import com.zh.model.task.home.SwipingCardResBean;
import com.zh.model.task.home.ValidateBean;
import com.zh.model.task.home.ValidateResBean;
import com.zh.model.task.relation.AssociateTag;
import com.zh.model.task.subject.LoginResBean;


import java.io.DataOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import retrofit2.http.Body;
import retrofit2.http.Query;

public class TaskModel {
    private Context context;
    public TaskModel(Context context){
        this.context = context;
    }
    /**
     * 设备心跳
     */
    public Observable<BaseResp<Object>> heartbeat(String equipmentCode) {
        return HRetrofitNetHelper.retrofit().create(ITaskLoginService.class).heartbeat(equipmentCode);
    }

    /**
     * 设备登录
     */
    public Observable<BaseResp<TokenBean>> equipmentLogin(EquipmentLogin equipmentLogin) {
        return HRetrofitNetHelper.retrofit().create(ITaskLoginService.class).equipmentLogin(equipmentLogin)
                .doOnNext(new Consumer<BaseResp<TokenBean>>() {
                    @Override
                    public void accept(BaseResp<TokenBean> baseResp) throws Exception {
                        if (baseResp.getData() != null && baseResp.getData().getToken() != null) {
                            BaseConstant.DEVICE_TOKEN = baseResp.getData().getToken();
                            ShareManager.setValue(context, BaseConstant.AUTHORIZATION_DEADLINE, baseResp.getData().getAuthorizationTime());

                            long lt = Long.valueOf(baseResp.getData().getCurrentTime());
                             BaseConstant.timeDifferent = lt - System.currentTimeMillis();

                        }
                    }
                });
    }

    /**
     * 柜子详细
     */
    public Observable<BaseResp<ClassCabinetBean>> detail(String schoolId, String equipmentCode) {
        if (BaseConstant.hasNet)
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).detail(schoolId, equipmentCode);
        else {
            BaseResp<ClassCabinetBean> baseResp = new BaseResp<ClassCabinetBean>();
            ClassCabinetBean mBean = JsonUtils.stringToObject(ShareManager.getValue(context, BaseConstant.LOCAL_CAB_LIST), ClassCabinetBean.class);
            baseResp.setData(mBean);
            baseResp.setCode(0);
            return Observable.just(baseResp);
        }
    }

    /**
     * 课代表刷卡登录
     */
    public Observable<BaseResp<LoginResBean>> login(String schoolId, LoginInfo info) {
        if (BaseConstant.hasNet)
            return HRetrofitNetHelper.retrofit().create(ITaskLoginService.class).login(schoolId, info);
        else {
            String code = info.getCardCode();
            ClassCabinetBean classCabinetBean = JsonUtils.stringToObject(ShareManager.getValue(context, BaseConstant.LOCAL_CAB_LIST), ClassCabinetBean.class);
            List<CabinetBean> cabinetDetailList = classCabinetBean.getCabinetDetailList();
            LoginResBean loginResBean = new LoginResBean();
            List<SubjectBean> cabinetDetailVOList = new ArrayList<>();
            for (CabinetBean one : cabinetDetailList) {
                if(one.getStuVOS()!=null)
                for(CabinetBean.StuVOSBean stuVOSBean : one.getStuVOS()) {
                    if (stuVOSBean.getStuCardCode().equals(code)) {
                        loginResBean.setName(stuVOSBean.getStuName());
                        SubjectBean subjectBean = new SubjectBean();
                        subjectBean.setCabinetNum(one.getCabinetNum());
                        subjectBean.setSubjectName(one.getSubjectName());
                        subjectBean.setTeacherName(one.getTeacherName());
                        cabinetDetailVOList.add(subjectBean);
                    }
                }
            }
            loginResBean.setCabinetDetailVOList(cabinetDetailVOList);
            BaseResp<LoginResBean> baseResp = new BaseResp<LoginResBean>();
            baseResp.setData(loginResBean);
            if (cabinetDetailVOList.size() != 0) {
                baseResp.setCode(0);
            } else {
                baseResp.setCode(-1);
                baseResp.setMsg("您非本柜班级对应科代表，刷卡失败");
            }
            return Observable.just(baseResp);
        }
    }

    /**
     * 勘误刷卡
     */
    public Observable<BaseResp<com.zh.model.task.error.ErrataSwipeResBean>> errataSwipe(ErrataSwipeBean cardCodeVO) {
        return HRetrofitNetHelper.retrofit().create(ITaskLoginService.class).errataSwipe(cardCodeVO);
    }

    /**
     * 关联作业刷卡登录
     */
    public Observable<BaseResp<SwipingCardResBean>> swipingCard(SwipingCardBean cardCodeVO) {
        return HRetrofitNetHelper.retrofit().create(ITaskLoginService.class).swipingCard(cardCodeVO);
    }

    /**
     * 课代表取出作业
     */
    public Observable<BaseResp<Object>> complete(String schoolId, CommitBean info) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).complete(schoolId, info);
    }

    /**
     * 提交作业
     */
    public Observable<BaseResp<CommitResBean>> commit(String schoolId, CommitBean info) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).commit(schoolId, info);
    }

    /**
     * 获取该柜子（科目）误投明细 V1.2.2
     */
    public Observable<BaseResp<ErrorDetailBean>> errorDetail(ErrorDetailVOBean versionBeano) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).detail(versionBeano);
    }

    /**
     * 勘误完成
     */
    public Observable<BaseResp<Object>> errorComplte(ErrorDetailVOBean versionBean) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).complte(versionBean);
    }

    /**
     * 校验射频标签
     */
    public Observable<BaseResp<ValidateBean>> identifyCode(ValidateResBean radioCode) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).validate(radioCode);
    }

    /**
     * 关联作业本
     */
    public Observable<BaseResp<Object>> associate(String schoolId, List<String> radioCodes) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).associate(schoolId, radioCodes);
    }
    /**
     * 关联作业本通用
     */
    public Observable<BaseResp<Object>> associateCurrencyTag(String schoolId,  List<AssociateTag> associateTagVO) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).associateCurrencyTag(schoolId, associateTagVO);
    }

    /**
     * 上报设备版本号
     */
    public Observable<BaseResp<Object>> reportVersion(String equipmentCode, ReportVersionBean versionBean) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).reportVersion(equipmentCode, versionBean);
    }

    /**
     * 远程开箱结果
     */
    public Observable<BaseResp<Object>> remoteOpenResult(RemoteOpenResVO remoteOpenResVO) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).remoteOpenResult(remoteOpenResVO);
    }
    /**
     * 开箱成功
     */
    public Observable<BaseResp<Object>> openSuccess(Errata errata) {
        return HRetrofitNetHelper.retrofit().create(ITaskBusiness.class).openSuccess(errata);
    }
}
